package com.itheima.mall.api.ums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ums.UmsResourceMapper;
import com.itheima.mall.pojo.ums.UmsResource;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class UmsResourceServiceImpl implements UmsResourceServiceApi{

    @Autowired
    UmsResourceMapper umsResourceMapper;

    @Override
    public R UmsResourceCreate(UmsResource umsResource) {
        umsResource.setCreateTime(LocalDateTime.now());
        int flag = umsResourceMapper.insert(umsResource);
        if (flag>0){
            return R.succeed("","创建成功");
        }

        return R.error("创建失败");
    }

    @Override
    public R UmsResourceDelete(Long id) {
        int flag = umsResourceMapper.deleteById(id);

        if (flag>0){
            return R.succeed("","删除成功");
        }

        return R.error("删除失败");
    }

    @Override
    public R UmsResourceList(Long categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {

        Page pageCondition = new Page(Convert.toLong(pageNum),Convert.toLong(pageSize));

        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(categoryId),UmsResource::getCategoryId,categoryId);
        lqw.like(ObjectUtil.isNotNull(nameKeyword),UmsResource::getName,nameKeyword);
        lqw.like(ObjectUtil.isNotNull(urlKeyword),UmsResource::getUrl,urlKeyword);

        Page pageR = umsResourceMapper.selectPage(pageCondition, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));

        return R.succeed(pageResult,"查询成功");
    }

    @Override
    public R UmsResourceListAll() {
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        List<UmsResource> umsResources = umsResourceMapper.selectList(lqw);

        return R.succeed(umsResources,"查找成功");
    }

    @Override
    public R UmsResourceUpdate(Long id, UmsResource umsResource) {
        umsResource.setId(id);
        int flag = umsResourceMapper.updateById(umsResource);

        if (flag>0){
            return R.succeed("","更新成功");
        }

        return R.error("更新失败");
    }

    @Override
    public R UmsResourceFindById(Long id) {

        UmsResource umsResource = umsResourceMapper.selectById(id);
        if (ObjectUtil.isNotNull(umsResource)){
            return R.succeed(umsResource,"查找成功");
        }
        return R.error("查找失败");
    }


}
